package user_center.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cwb.utils.AesUtil;
import cwb.utils.CryptoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import user_center.user.mapper.SystemUserMapper;
import user_center.user.pojo.SystemUser;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Ezio.Auditore
 * @since 2021-12-20
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser> {

    @Autowired
    SystemUserMapper systemUserMapper;

    /**
     * 登录方法
     *
     * @param username
     * @param password
     * @return
     */
    public String login(String username, String password) {
        SystemUser user = systemUserMapper.selectByUserName(username);
        if (user == null) {
            return "登录失败，用户信息不存在";
        } else {
            String salt = user.getUserSalt();
            String dbPassword = AesUtil.decrypt(user.getUserPassword(), salt);
            log.debug("db密码：" + dbPassword);
            log.debug("输入密码：" + dbPassword);
            if (dbPassword.equals(password)) {
                return "登录成功";
            }
        }
        return null;
    }

    /**
     * 根据用户名查找用户信息
     *
     * @param username
     * @return
     * @throws Exception
     */
    public SystemUser getByName(String username) throws Exception {
        SystemUser systemUser = systemUserMapper.selectByUserName(username);
        String salt = systemUser.getUserSalt();
        String decrypt = AesUtil.decrypt(systemUser.getUserPassword(), salt);
        systemUser.setUserPassword(decrypt);
        systemUser.setUserSalt(null);
        return systemUser;
    }

    /**
     * 新增用户
     *
     * @param systemUser
     * @return
     */
    public boolean addNewUser(SystemUser systemUser) throws Exception {
        //查询用户名是否存在
        if (null != systemUserMapper.selectByUserName(systemUser.getUserName())) {
            throw new Exception("用户名已存在");
        }
        //查询手机号是否存在
        if (null != systemUserMapper.selectByPhone(systemUser.getUserPhone())) {
            throw new Exception("手机号已存在");
        }
        //查询邮箱是否存在
        if (null != systemUserMapper.selectByEmail(systemUser.getUserEmail())) {
            throw new Exception("邮箱已存在");
        }
        //查询QQ是否存在
        if (null != systemUserMapper.selectByQQ(systemUser.getUserQq())) {
            throw new Exception("QQ已存在");
        }
        //随机生成Aes秘钥作为盐值
        String salt = CryptoUtil.genAesSecret();
        String password = systemUser.getUserPassword();

        log.debug("盐：" + salt + "   密码：" + password);
        //将密码进行Aes加密
        try {
            String decryptPassword = AesUtil.encrypt(password, salt);
            log.debug("加密后的密码：" + decryptPassword);
            //将加密后的盐和秘钥set到新增的对象中
            systemUser.setUserPassword(decryptPassword);
            systemUser.setUserSalt(salt);
            //新增用户
            int insert = systemUserMapper.insert(systemUser);
            if (insert > 0) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.error("新增用户失败，原因", e);
            return false;
        }
    }
}
